<?php
namespace app\common\api;
//ob_start();  //打开缓存空间
use app\common\controller\App;
use think\Cache;
class Wechat extends App
{
    protected $is_trace=false;
    const TOKEN = 'token_admin';//类常量TOKEN
    protected $token;
    //protected $appId = 'wx52a624095e74bdca';//攀登：wx52a624095e74bdca//狗哥：wx8ae2532c5be67598
    //protected $appSecret = 'b4e690140a57d8378b99cba75a40625d';//攀登'b4e690140a57d8378b99cba75a40625d';//狗哥：7b9e6ec093f7ae0913255551f29ac770
    protected $appId = 'wx3b2ff8ef5b4b0cde'; //自己
    protected $appSecret = '1dfdcffcdd1f4a802b2be1ead9fe144e';
    private $fromUsername;
    private $toUsername;
    private $time;
    private $MsgType;
    private $msgType = "text";
    /**
     * 获取access_token
     */
    public static function aa(){
        return 11;
    }
    public function getAccessToken(){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
        $is_Cached = false;
        $token = Cache::get('token');
        //$token = false;
        if($token){
            $is_Cached = true;
        }else{
            $result = ihttp_get($url);## 调用接口
            $result = json_decode($result['content'], true);
            if(isset($result['access_token'])){
                $token = $result['access_token'];
                $is_Cached = true;
                Cache::set('token',$result['access_token'],$result['expires_in']-200);
            }else{
                die('接口调用失败'.$result['errcode']);
            }
        }
        $this->token = $token;
        return $token;
    }
    
    /*public function getCache(){
        return \Cache::get('OBJ');
    }*/
    //签名验证程序    ，checkSignature被18行调用。官方加密、校验流程：将token，timestamp，nonce这三个参数进行字典序排序，然后将这三个参数字符串拼接成一个字符串惊喜shal加密，开发者获得加密后的字符串可以与signature对比，表示该请求来源于微信。
    protected function checkSignature()
    {
        $signature = $_GET["signature"];//从用户端获取签名赋予变量signature
        $timestamp = $_GET["timestamp"];//从用户端获取时间戳赋予变量timestamp
        $nonce = $_GET["nonce"];    //从用户端获取随机数赋予变量nonce

        $token = self::TOKEN;//将常量token赋予变量token
        $tmpArr = array($token, $timestamp, $nonce);//简历数组变量tmpArr
        sort($tmpArr, SORT_STRING);//新建排序
        $tmpStr = implode( $tmpArr );//字典排序
        $tmpStr = sha1( $tmpStr );//shal加密
        //tmpStr与signature值相同，返回真，否则返回假
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
    //生成随机字符串
    public function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
	//获取jsApi
    protected function getJsApiTicket() {
        // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例
        if(Cache::get('jsapi_ticket')){
            return Cache::get('jsapi_ticket');
        }
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$this->token}&type=jsapi";
        $ticket = ihttp_get($url);
        $ticket = json_decode($ticket['content'],true);
        $ticket = $ticket['ticket'];
        Cache::set('jsapi_ticket',$ticket,7000);
        return $ticket;
    }

}